﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Xml;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.CommandWpf;

namespace WpfApp4DataGrid.views
{
    /// <summary>
    /// ReadXmlView.xaml 的交互逻辑
    /// 参考文章：https://www.jb51.net/article/34451.htm
    /// 文章2：https://zhuanlan.zhihu.com/p/369888626
    /// </summary>
    public partial class ReadXmlView : Window
    {
        public ReadXmlView()
        {
            InitializeComponent();
        }

        // 生成文件
        private void Button_Click_C(object sender, RoutedEventArgs e)
        {
            XmlDocument xml = new XmlDocument();
            // create description info
            XmlDeclaration header = xml.CreateXmlDeclaration("1.0", "utf-8", null);
            xml.AppendChild(header);

            // 内容
            XmlElement root = xml.CreateElement("bookStore");
            XmlAttribute bookStoreTitle = xml.CreateAttribute("title");
            bookStoreTitle.InnerText = "我的微信：yizheng369";
            root.SetAttributeNode(bookStoreTitle);

            // son
            XmlElement son = xml.CreateElement("one");
            XmlAttribute sonName = xml.CreateAttribute("name");
            son.SetAttributeNode(sonName);
            sonName.InnerText = "tom";
            // 添加儿子
            root.AppendChild(son);

            // son2
            XmlElement son2 = xml.CreateElement("one");
            XmlAttribute sonName2 = xml.CreateAttribute("name");
            son2.SetAttributeNode(sonName2);
            sonName2.InnerText = "tom2";
            // 添加儿子
            root.AppendChild(son2);

            // 放入根节点
            xml.AppendChild(root);

            // 最后保存文件
            var fileName = @"D://demoXml.xml";
            xml.Save(fileName);
            MessageBox.Show("生成文件成功");
        }

        // 读取
        private void Button_Click_R(object sender, RoutedEventArgs e)
        {
            Console.WriteLine("sender" + sender);
            XmlDocument xml = new XmlDocument();

            var fileName = @"D://demoXml.xml";
            xml.Load(fileName); //导入指定xml文件
            XmlNode xRoot = xml.SelectSingleNode("/bookStore");
            if (xRoot != null)
            {
                XmlNodeList xNList = xRoot.ChildNodes;//获取节点下所有直接子节点
                String nameStr = "";
                foreach (XmlNode xCur in xNList)
                {
                    XmlElement eCur = (XmlElement)xCur;
                    String name = eCur.GetAttribute("name");
                    Console.WriteLine("sdf:" + name);
                    nameStr += name + " ";
                }
                // this.DataContext = nameArr;
                this.DataContext = new
                {
                    Model = nameStr
                };
                MessageBox.Show("读取文件成功");

            }
        }
    }
}
